package com.alibaba.citrus.cr.unified.inventory.functions.factory;

import com.alibaba.citrus.cr.unified.inventory.model.AllocateOrderEntity;
import com.alibaba.citrus.cr.unified.inventory.model.AllocateOrderEntityBatchComposeRequest;
import com.alibaba.citrus.cr.unified.inventory.model.AllocateOrderEntityComposeRequest;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crunifiedinventory.domain.allocateorder.dto.AllocateOrderLoadListRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorder.model.AllocateOrder;
import com.epoch.app.crunifiedinventory.domain.allocateorder.service.AllocateOrderService;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.dto.AllocateOrderDetailLoadListRequest;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.model.AllocateOrderDetail;
import com.epoch.app.crunifiedinventory.domain.allocateorderdetail.service.AllocateOrderDetailService;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @author: yuhw
 * @date: 2024年01月22日 20:40
 **/
@Component
public class AllocateOrderEntityFactory {

    private static final Integer MAX_PAGE_SIZE = 1000;
    @Resource
    private AllocateOrderService allocateOrderService;
    @Resource
    private AllocateOrderDetailService allocateOrderDetailService;

    public AllocateOrderEntity composeAllocateOrderEntity(AllocateOrderEntityComposeRequest request){
        if (request == null){
            return null;
        }
        AllocateOrderEntity allocateOrderEntity = new AllocateOrderEntity();
        AllocateOrderLoadListRequest loadListRequest = new AllocateOrderLoadListRequest();
        if (StringUtils.isNotBlank(request.getCode())){
            loadListRequest.setCode(request.getCode());
        }else if (StringUtils.isNotBlank(request.getOutAllocateOrderId())){
            loadListRequest.setOutAllocateOrderId(request.getOutAllocateOrderId());
        }else  {
            return null;
        }
        Result<List<AllocateOrder>> listResult = allocateOrderService.loadAllocateOrderList(loadListRequest);
        if (listResult == null || CollectionUtils.isEmpty(listResult.getResult())){
            return null;
        }
        AllocateOrder allocateOrder = listResult.getResult().get(0);
        allocateOrderEntity.setAllocateOrder(allocateOrder);
        AllocateOrderDetailLoadListRequest detailLoadListRequest = new AllocateOrderDetailLoadListRequest();
        detailLoadListRequest.setAllocateOrderId(allocateOrder.getId());
        detailLoadListRequest.setLimit(MAX_PAGE_SIZE);
        Result<List<AllocateOrderDetail>> allocateOrderDetailList = allocateOrderDetailService.loadAllocateOrderDetailList(detailLoadListRequest);
        if (allocateOrderDetailList != null && CollectionUtils.isNotEmpty(allocateOrderDetailList.getResult())){
            allocateOrderEntity.setDetailList(allocateOrderDetailList.getResult());
        }
        return allocateOrderEntity;
    }

    public List<AllocateOrderEntity> batchComposeAllocateOrderEntityList(AllocateOrderEntityBatchComposeRequest batchComposeRequest){
        if (batchComposeRequest == null){
            return null;
        }
        AllocateOrderLoadListRequest allocateOrderLoadListRequest = new AllocateOrderLoadListRequest();
        if (StringUtils.isNotBlank(batchComposeRequest.getRelatedAllocateOrderCode())){
            allocateOrderLoadListRequest.setRelatedAllocateOrderCode(batchComposeRequest.getRelatedAllocateOrderCode());
        }else {
            return null;
        }
        Result<List<AllocateOrder>> allocateOrderList = allocateOrderService.loadAllocateOrderList(allocateOrderLoadListRequest);
        if (allocateOrderList == null || CollectionUtils.isEmpty(allocateOrderList.getResult())){
            return null;
        }
        List<AllocateOrderEntity> allocateOrderEntities = Lists.newArrayList();
        allocateOrderList.getResult().forEach(allocateOrder -> {
            AllocateOrderEntity allocateOrderEntity = new AllocateOrderEntity();
            allocateOrderEntity.setAllocateOrder(allocateOrder);
            AllocateOrderDetailLoadListRequest detailLoadListRequest = new AllocateOrderDetailLoadListRequest();
            detailLoadListRequest.setAllocateOrderId(allocateOrder.getId());
            detailLoadListRequest.setLimit(MAX_PAGE_SIZE);
            Result<List<AllocateOrderDetail>> allocateOrderDetailList = allocateOrderDetailService.loadAllocateOrderDetailList(detailLoadListRequest);
            if (allocateOrderDetailList != null && CollectionUtils.isNotEmpty(allocateOrderDetailList.getResult())){
                allocateOrderEntity.setDetailList(allocateOrderDetailList.getResult());
            }
            allocateOrderEntities.add(allocateOrderEntity);
        });
        return allocateOrderEntities;
    }
}
